﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using GBG_DBM.Encryption;
using GBG_DBM.GlobalParameters;

namespace GBG_DBM.Properties
{


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
    {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default
        {
            get
            {
                return defaultInstance;
            }
        }

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        [global::System.Configuration.DefaultSettingValueAttribute("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\Database\\Database.accdb")]
        public string DatabaseConnectionString
        {
            get
            {
                //return Encrypt.DecryptDES((string)(this["DatabaseConnectionString"]), GlobalParams.EncryptKey);
                return Encrypt.DecryptDES((this["DatabaseConnectionString"].ToString()), GlobalParams.EncryptKey);
            }
            set
            {
                base["DatabaseConnectionString"] = Encrypt.EncryptDES(value, GlobalParams.EncryptKey);
                this.SetKeyValue("GBG_DBM.Properties.Settings.DatabaseConnectionString", Encrypt.EncryptDES(value, GlobalParams.EncryptKey));
            }
        }

        /// <summary>
        /// Set connection string
        /// </summary>
        /// <param name="AppKey">Application Key in setting.cs</param>
        /// <param name="AppValue">Key Value</param>
        private void SetKeyValue(string AppKey, string AppValue)
        {
            XmlDocument _xmlDoc = new XmlDocument();
            _xmlDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");

            XmlNode _xmlNode;
            XmlElement _xmlElementExt;
            XmlElement _xmlElementNoExt;

            _xmlNode = _xmlDoc.SelectSingleNode("//connectionStrings");

            _xmlElementExt = (XmlElement)_xmlNode.SelectSingleNode("//add[@name='" + AppKey + "']");

            if (_xmlElementExt != null)
            {
                _xmlElementExt.SetAttribute("connectionString", AppValue);
            }
            else
            {
                _xmlElementNoExt = _xmlDoc.CreateElement("add");
                _xmlElementNoExt.SetAttribute("name", AppKey);
                _xmlElementNoExt.SetAttribute("connectionString", AppValue);
                _xmlNode.AppendChild(_xmlElementNoExt);
            }
            try
            {
                _xmlDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
            }
            catch (Exception _Ex)
            {
                throw new Exception(_Ex.Message);
            }
        }
    }
}